import {
	$wuxKeyBoard
} from '../../dist/index'
var app = getApp();
var $ = app.globalData;
Page({
	data: {
		countries: ["快递到家", "到店自取"],
		countryIndex: 0,

		orderall3: 0,
		beizhi: "",
		shoptype:0
	},
	bindCountryChange: function(e) {
		console.log('picker country: ', e.detail.value);
		this.setData({
			countryIndex: e.detail.value
		});
	},
	onLoad(option) {
		var gowuce = option.gowuce ? true : false
		var daizhuce = option.daizhuce?option.daizhuce:false
		// 获取用户信息
		var that = this
		
		wx.getStorage({
			key: "userinfo",
			success: (res) => {
				if(!res.data.paypassword && res.data.identity){
					wx.navigateTo({
						url:"/pages/my/paypsw/paupsw"
					})
				}
				that.setData({
					shoptype:$.shoptype,
					gowuce: gowuce,
					userinfo: res.data,
					daizhuce:daizhuce
				})
				// 获取默认地址
				wx.request({
					url: $.AdminUrl + "/YApi/getMyAddress",
					data: {
						customerid: res.data.id
					},
					success: function(data) {
						if (data.data.obj.length > 0) {
							var thisxadd = data.data.obj[0]
							that.setData({
								thisxadd: thisxadd
							})
						}
					}
				})
			}
		})
		// 获取订单信息
		if (gowuce) {
			console.log("购物车")
			var orderall = 0
			var orderall1 = 0
			var orderall2 = 0
			var orderall3 = 0
			var shopid = ","
			var goodsids = ","
			var counts = ","
			var goodsspecids = ","
			// 购物车
				that.setData({
					order: $.orderCart
				})
				for (var i = 0; i < $.orderCart.length; i++) {
						orderall += $.orderCart[i].vipcashprice * $.orderCart[i].count
						orderall1 += $.orderCart[i].vipintegralprice * $.orderCart[i].count
						orderall2 += $.orderCart[i].plainprice * $.orderCart[i].count
						orderall3 += $.orderCart[i].fareprice * $.orderCart[i].count
						shopid += $.orderCart[i].shopid
						goodsids += $.orderCart[i].goodsid + ","
						counts += $.orderCart[i].count + ","
						goodsspecids += $.orderCart[i].specid + ","
					}
					that.setData({
						orderall: orderall,
						orderall1: orderall1,
						orderall2: orderall2,
						orderall3: orderall3,
						shopid: shopid,
						goodsids: goodsids,
						counts: counts,
						goodsspecids: goodsspecids,
						shoptype:$.orderCart[0].saletype
					})
		} else {
			// 非购物车
			console.log("非购物车")
			that.setData({
				order: $.orderarr,
				shoptype:$.orderarr.shoptype,
				goodsnub:$.orderarr.nub
			})
			if (!$.orderarr.shopid) {
				that.setData({
					countryIndex:0
				})
			}
		}
	},
	// 备注
	beizhu: function(e) {
		this.setData({
			beizhi: e.detail.value
		})
	},
	// 密码输入
	open() {
		var that = this
		if (!that.data.thisxadd) {
			wx.showToast({
				icon: 'none',
				title:"你还没有选择地址哦~",
				duration: 3000,
			})
		}else if(that.data.userinfo.identity != 0){
			// 钱包普通钱包支付
			$wuxKeyBoard().show({
				className: 'givingBut',
				inputText: '请输入支付密码',
				disorder: true,
				callback(value) {
					wx.showLoading({
						title: '购买中...',
					})
					console.log(value)
					wx.getStorage({
						key: "openid",
						success: (res) => {
							// console.log(that.data.userinfo)
							// 普通钱包支付
							if (!that.data.gowuce) {
								// 判断是否是代理注册
								console.log(that.data.daizhuce)
								if(that.data.daizhuce == "true"){
									var yunfei = that.data.countryIndex?0:that.data.order.yunfei * that.data.goodsnub
									wx.request({
										url: $.AdminUrl + "/YApi/helpregister",
										data: {
											customerid: that.data.userinfo.id,
											receiveid: that.data.thisxadd.id,
											shopid: that.data.order.shopid,
											goodscashprice: that.data.order.jiage * that.data.goodsnub,
											goodsintegralprice: that.data.order.jiage2 * that.data.goodsnub,
											expressprice:yunfei,
											totalprice: that.data.order.jiage * that.data.goodsnub +yunfei,
											remarks: that.data.beizhi,
											shoptype: that.data.order.shoptype,
											expresstype: that.data.countryIndex,
											isshopcar: 0,
											openid: res.data,
											goodsids: that.data.order.goods,
											counts: that.data.goodsnub,
											goodsspecids: that.data.order.guigeid,
											phone:$.daizhuce['phone'],
											name:$.daizhuce['name'],
											recommendcode:$.daizhuce['yqcode'],
											paypassword: value
										},
										success: function(data) {
											wx.hideLoading()
											if (data.data.code) {
												wx.navigateTo({
													url: "../result/success?orderid=" + data.data.obj.id
												})
											} else {
												wx.showToast({
													icon: 'none',
													title: data.data.msg,
													duration: 3000,
												})
											}
										}
									})
								}else{
									var yunfei = that.data.countryIndex?0:that.data.order.yunfei * that.data.goodsnub
									wx.request({
										url: $.AdminUrl + "/YApi/payOrderByWallet",
										data: {
											customerid: that.data.userinfo.id,
											receiveid: that.data.thisxadd.id,
											shopid: that.data.order.shopid,
											goodscashprice: that.data.order.jiage * that.data.goodsnub,
											goodsintegralprice: that.data.order.jiage2 * that.data.goodsnub,
											expressprice:yunfei,
											totalprice: that.data.order.jiage * that.data.goodsnub + yunfei,
											remarks: that.data.beizhi,
											shoptype: that.data.order.shoptype,
											expresstype: that.data.countryIndex,
											isshopcar: 0,
											openid: res.data,
											goodsids: that.data.order.goods,
											counts: that.data.goodsnub,
											goodsspecids: that.data.order.guigeid,
											paypassword: value
										},
										success: function(data) {
											wx.hideLoading()
											if (data.data.code) {
												wx.navigateTo({
													url: "../result/success?orderid=" + data.data.obj
												})
											} else {
												wx.showToast({
													icon: 'none',
													title: data.data.msg,
													duration: 3000,
												})
											}
										}
									})
								}
							} else {
								var goodslist = that.data.order
								var counts = ","
								for(var i=0;i<goodslist.length;i++){
									counts += goodslist[i].count + ","
								}
								var yunfei = that.data.countryIndex?0:that.data.orderall3
								// 购物车支付
								wx.request({
									url: $.AdminUrl + "/YApi/payOrderByWallet",
									data: {
										customerid: that.data.userinfo.id,
										receiveid: that.data.thisxadd.id,
										shopid: that.data.order[0].shopid,
										goodscashprice: that.data.orderall,
										goodsintegralprice: that.data.orderall1,
										expressprice:yunfei ,
										totalprice: that.data.orderall + yunfei,
										remarks: that.data.beizhi,
										shoptype: that.data.order[0].saletype,
										expresstype: that.data.countryIndex,
										isshopcar: 1,
										openid: res.data,
										goodsids: that.data.goodsids,
										counts: counts,
										goodsspecids: that.data.goodsspecids,
										paypassword: value
									},
									success: function(data) {
										wx.hideLoading()
										if (data.data.code) {
											wx.navigateTo({
												url: "../result/success?orderid=" + data.data.obj
											})
										} else {
											wx.showToast({
												icon: 'none',
												title: data.data.msg,
												duration: 3000,
											})
										}
									}
								})
							}
						}
					})
				}
			})
		}else{
			if (!that.data.gowuce) {
				var jiage2 = that.data.order.shoptype == 0?0:0
				var jiage3 = that.data.order.shoptype == 0?that.data.order.jiage3 * that.data.goodsnub:that.data.order.jiage * that.data.goodsnub
				console.log(that.data.order)
				wx.showLoading({
					title: '购买中...',
				})
				wx.getStorage({
					key: "openid",
					success: (res) => {
					var yunfei = that.data.countryIndex?0: that.data.order.yunfei * that.data.goodsnub
					wx.request({
						url: $.AdminUrl + "/YApi/payOrderByWx",
						data: {
							customerid: that.data.userinfo.id,
							receiveid: that.data.thisxadd.id,
							shopid: that.data.order.shopid,
							goodscashprice:jiage3,
							goodsintegralprice: jiage2,
							expressprice:yunfei,
							totalprice: jiage3 + yunfei,
							remarks: that.data.beizhi,
							shoptype: that.data.order.shoptype,
							expresstype: that.data.countryIndex,
							isshopcar: 0,
							openid: res.data,
							goodsids: that.data.order.goods,
							counts: that.data.goodsnub,
							goodsspecids: that.data.order.guigeid,
							},
						success: function(data) {
							wx.hideLoading()
							wx.requestPayment({
								timeStamp:data.data.timeStamp,
								nonceStr:data.data.nonceStr,
								package:data.data.package,
								signType: 'MD5',
								paySign:data.data.paySign,
								success: function(event) {
									wx.showToast({
										icon: 'none',
										title: "购买成功",
										duration: 3000,
									})
									console.log(event.errMsg)
									console.log(event)
									setTimeout(function(){
										wx.switchTab({url:"../index/index"})
									},3000)
								},
							})
						}
					})
				},
			})
			}else{
				var goodslist = that.data.order
				var counts = ","
				for(var i=0;i<goodslist.length;i++){
					counts += goodslist[i].count + ","
				}
					wx.showLoading({
						title: '购买中...',
					})
					wx.getStorage({
						key: "openid",
						success: (res) => {
						var yunfei = that.data.countryIndex?0: that.data.orderall3
						wx.request({
							url: $.AdminUrl + "/YApi/payOrderByWx",
							data: {
								customerid: that.data.userinfo.id,
								receiveid: that.data.thisxadd.id,
								shopid: that.data.order[0].shopid,
								goodscashprice: that.data.orderall2,
								goodsintegralprice: 0,
								expressprice:yunfei,
								totalprice: that.data.orderall2 + yunfei,
								remarks: that.data.beizhi,
								shoptype: that.data.order[0].saletype,
								expresstype: that.data.countryIndex,
								isshopcar: 1,
								openid: res.data,
								goodsids: that.data.goodsids,
								counts: counts,
								goodsspecids: that.data.goodsspecids,
								},
							success: function(data) {
								wx.hideLoading()
								wx.requestPayment({
									timeStamp:data.data.timeStamp,
									nonceStr:data.data.nonceStr,
									package:data.data.package,
									signType: 'MD5',
									paySign:data.data.paySign,
									success: function(event) {
										wx.showToast({
											icon: 'none',
											title: "购买成功",
											duration: 3000,
										})
										console.log(event.errMsg)
										console.log(event)
										setTimeout(function(){
											wx.switchTab({url:"../index/index"})
										},3000)
									},
								})
							}
						})
					},
				})
			}
		}
	},
	// 数字变化
	nubchange: function(e) {
		// console.log(e.detail.value)
		this.setData({
			goodsnub: e.detail.value
		})
	},
	// 购物车
	cartnub: function(e) {
		var that = this
		var ind = e.currentTarget.dataset.index
		var val = e.detail.value
		var orderlist = that.data.order
		var orderall = 0
		var orderall1 = 0
		var orderall2 = 0
		var orderall3 = 0
		orderlist[ind].count = val
		
		for (var i = 0; i < orderlist.length; i++) {
			orderall += orderlist[i].vipcashprice * orderlist[i].count
			orderall1 += orderlist[i].vipintegralprice * orderlist[i].count
			orderall2 += orderlist[i].plainprice * orderlist[i].count
			orderall3 += orderlist[i].fareprice * orderlist[i].count
		}
		
		that.setData({
			orderall: orderall,
			orderall1: orderall1,
			order: orderlist,
			orderall2:orderall2,
			orderall3:orderall3
		})
	},
	addclick: function() {
		wx.navigateTo({
			url: "../my/myadd/myadd?navgat=true"
		})
	},
	onShow() {
		var that = this
		wx.getStorage({
			key: "addid",
			success: (res) => {
				wx.request({
					url: $.AdminUrl + "/YApi/getOneAddress",
					data: {
						addressid: res.data
					},
					success: (data) => {
						that.setData({
							thisxadd: data.data.obj
						})
					}
				})
			}
		})
	}
})
